package collector;

import obj.Accum;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class AccumsPriceCollector {
    private static final String OUT_DATE_FORMAT = "dd-MM-yyyy-HH-mm";
    static SimpleDateFormat outFormat = new SimpleDateFormat(OUT_DATE_FORMAT);

    public static String yandexCookie = "spravka=dD0xMzE0NTI5NDcwO2k9MTA5LjIwNS4yNTMuMTM1O3U9MTMxNDUyOTQ3MDA4MjMxMjAzNDtoPTFlZWM1ZDk2ZjE5MzE3Nzk3ZmYzMGZmNzVjNTAzMjA3; yandexuid=2027961314420485; fuid01=4d726ae12b1a1ac0.N9hJY3OOtWG-kEtar5NvfvMUs1Xo_8AUqbE-fG1uUYhwf2PZq1e_rMNyTIPcAliV5rOj5oRQaNMCW7Z2qn2HXaeA_Otjy40U2yeL3hFKkrbBAo6y88nb2eM2jOvZk-KQ; yabs-frequency=/3/derY07098Tcj0mfp2KS000B_0WGFS0bXTm805sm9t000/; yp=1630729273.sp.nd:50:lang:!#1629890300.udn.dG9ueWdsb3ZhdHNreQ==; my=YycCAAAtAQIA; L=WEEBEHx6ZGVCWl5qcgYAU3sEW1x+X1BdHyUZKUAOdRUGBD93NEFNNwFVWVQREyAcIQEvPxguDCAWPTs7FRBAJw==.1314530300.9094.248914.7bddf229a1bd02dc832392d082dde666; aw=1_beJxi5GVgENwlyFGyuoj5PwAAAP//AwAPEQOG#A#; t=p; ys=udn.dG9ueWdsb3ZhdHNreQ==; yandex_login=tonyglovatsky; Session_id=1314531647.5324.0.125472376.2:355607797:330.8:1314530300346:1842216327:122.34:355607796.77952.7060.07daefa0306b4aa4157d70d85697bccf";

    public static void main(String[] args) throws IOException, InterruptedException {
        PrintStream out = new PrintStream(new FileOutputStream("prices/accums.nokia.price." + outFormat.format(new Date()) + ".txt"));
        File folder = new File("data");
        String domain = "sotmarket.ru";
        for (File file : folder.listFiles()) {
            if (!file.getName().contains("accum") ||
                    file.getName().contains("maxcell") ||
                    !file.getName().endsWith(".txt")) {
                continue;
            }
            BufferedReader reader = new BufferedReader(new FileReader(file));
            String line;
            while ((line = reader.readLine()) != null) {
                Accum accum = Accum.parseAccum(line, 1);
                String phrase = processPhrase(accum.name);
                String price;
                try {
                    price = requestYandexPrice(phrase, domain);
                } catch (IOException e) {
                    processCaptcha();
                    price = requestYandexPrice(phrase, domain);
                }
                System.out.println(accum.name + ":" + price);
                out.println(accum.name + ":" + price);
                delay();
            }
            reader.close();
            System.out.println("------------------------------------------");
            out.println("------------------------------------------");
        }
        out.close();
    }

    public static String requestYandexPrice(String phrase, String domain) throws IOException, InterruptedException {
        Document doc;
        String url = "http://yandex.ru/yandsearch?p=0&text=" + phrase + "&lr=2" + "&numdoc=50";
        System.out.println(url);
        doc = Jsoup.connect(url).
                header("user-agent", "Mozilla/5.0 (Windows NT 5.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1").
                header("Cookie", yandexCookie).
                timeout(20000).get();
        Element itemList = doc.select("ol.b-serp-list").get(0);
        Elements items = itemList.select(".b-serp-item");
        for (Element item : items) {
            String linkDomain = item.select("a.b-serp-url__link").get(0).text();
            if (linkDomain.equals(domain)) {
                String goodUrl = item.select("a.b-serp-item__title-link").get(0).attr("href");
                String realPrice = getSotmarketPrice(goodUrl);
                try {
                    return item.select("span.b-market__price").get(0).text() + "; " + realPrice;
                } catch (IndexOutOfBoundsException e) {
                    return item.select("div.b-serp-item__text").get(0).text() + "; " + realPrice;
                }
            }
        }
        return "";
    }

    public static String getSotmarketPrice(String url) throws IOException {
        Document doc = Jsoup.connect(url).
                header("user-agent", "Mozilla/5.0 (Windows NT 5.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1").
                timeout(20000).get();
        Elements elems = doc.select("input.greenCR");
        if (elems.size() == 0) {
            return "unknown";
        }
        String price = elems.get(0).attr("value");
        return price;
    }

    private static void processCaptcha() throws IOException, InterruptedException {
        System.out.println("GOT CAPTCHA!!!");
        InputStreamReader in = new InputStreamReader(System.in);
        String newCookie = new BufferedReader(in).readLine();
        yandexCookie = newCookie;
    }

    static int parsePrice(String price) {
        Pattern p = Pattern.compile("\\d+");
        Matcher m = p.matcher(price);
        m.find();
        return Integer.parseInt(price.substring(m.start(), m.end()));
    }

    public static void delay() throws InterruptedException {
//        int delay = 1500 + (int) (Math.random() * 1200);
//        Thread.sleep(delay);
        Thread.sleep(5000);
    }

    public static String processPhrase(String phrase) throws UnsupportedEncodingException {
        if (phrase.charAt(0) == 65279) {
            phrase = phrase.substring(1);
        }
        phrase = phrase.replaceAll(" ", "+");
        return phrase;
    }
}
